home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11440 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  93 lines

  1. Path: digex.net!not-for-mail
  2. From: jdc@access2.digex.net (John Cochran)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: longjmp/setjmp madness
  5. Date: 24 Mar 1996 02:47:40 -0500
  6. Organization: Express Access Online Communications, Greenbelt, MD USA
  7. Message-ID: <4j2ums$1tb@access2.digex.net>
  8. References: <4j0llr$rvl@gap.cco.caltech.edu>
  9. NNTP-Posting-Host: access2.digex.net
  10.  
  11. In article <4j0llr$rvl@gap.cco.caltech.edu>,
  12. Mika Nystroem <mika@ariadne.cs.caltech.edu> wrote:
  13. >
  14. >
  15. >Hi everyone,
  16. >  I have been trying to write a nasty little program here, and had some
  17. >problems. I have managed to distill my problem to the following 
  18. >unpleasantness. 
  19. >  The man pages are a little sketchy on how longjmp and setjmp are supposed
  20. >to work, but I don't *think* I have violated the specification. If I have,
  21. >please correct me on that point. (I know that what this program is doing
  22. >is incredibly nasty, and at least in this case, could be done much
  23. >nicer.. I just want to know if it is written according to specs.)
  24. >This program compiles and executes without problems on
  25. >   sparc/SunOS 4.1.4
  26. >   AXP/DEC UNIX 3.2
  27. >
  28. >And compiles and executes with a segfault upon leaving p1() on
  29. >   i386/NetBSD 1.1A
  30. >   i386/Linux  1.3.x
  31. >   i386/NeXTstep
  32. >   sparc/NetBSD 1.1B
  33. >
  34. >Ho hum.
  35. >   Mika
  36. >   <mika@vlsi.cs.caltech.edu>
  37. >
  38. >P.S. If you wish to respond to me, please Cc: me in email. Thanks!
  39. >
  40. >--- nasty begins here ---
  41. >/* ltest.c 
  42. >   mika@vlsi 
  43. > */
  44. >
  45. >#include <setjmp.h>
  46. >#include <stdio.h>
  47. >
  48. >jmp_buf e1,e2,e3,e4;
  49. >
  50. >void p1()
  51. >{
  52. >  if(!setjmp(e2)) longjmp(e1,1);
  53. >  printf("p1 returning now\n"); fflush(stdout);
  54. >  return;
  55. >}
  56. >  
  57. >void
  58. >main() 
  59. >{
  60. >  if(!setjmp(e1)) {
  61. >    printf("calling p1\n");
  62. >    p1();
  63. >    printf("returning from p1()\n");
  64. >    exit(0);
  65. >  }
  66. >  printf("longjmping to p1/e2\n");
  67. >  longjmp(e2,1);
  68. >}
  69. >
  70.  
  71. Sorry, but you have violated the constraints on longjmp.
  72.  
  73. Quoting Section 7.6.2.1 ISO Standard C
  74.  
  75. Description
  76.   The longjmp function restores the environment saved by the most recent
  77. invocation of the setjmp macro in the same invocation of the program, with
  78. the corresponding jmp_buf argument.  If there has been no such invocation, or
  79. if the function containing the invocation of the setjmp macro has terminated
  80. execution in the interim, the behaivor is undefined.
  81.  
  82. ----
  83.  
  84. As soon as p1() returns, jmp_buf e2 becomes invalid because function p1() has
  85. terminated execution.  Therefore, the longjmp attempting to enter p1() will
  86. cause undefined behaivor.
  87.  
  88. Hope this helps,
  89. John Cochran
  90.  
  91.  
  92.